# logger_service.rb
# The logger service will:
# * Read a message from an AMQP queue
# * Parse this message (probably in xml)
# * Store the relevant information in a database
# Persistance will be achieved using active record
require 'rubygems'
require 'mq'
require 'yaml'

if ARGV.size > 0
  config_file = ARGV[0] 
else
  config_file = 'config.yml'
end

puts "using #{config_file}"

# reads configuration file
configuration = YAML::load(File.open(config_file))

# loads processor class
require configuration[:processor_filename]

# instantiates processor class
processor = Object::const_get(configuration[:processor_classname]).new

# start listening to queue 
EM.run {
  #mq server configuration
  AMQP.start(:host => configuration[:amqp_host], :port => configuration[:amqp_port]) do
    amq = MQ.new
    # for each new message execute process
    amq.queue(configuration[:amqp_queue_name]).subscribe{ |msg| processor.process(msg) }
    #grecefully shutsdown
    trap("INT") do
      puts "captured ctrl-c"
      AMQP.stop {EM.stop}
      Kernel.exit(0)
    end
  end
}
